package com.ruoyi.project.modules.interfaceModel.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.modules.interfaceinfo.domain.FieldModelDto;
import com.ruoyi.project.modules.interfaceinfo.domain.InterfaceInfo;
import com.ruoyi.project.modules.interfaceinfo.domain.VirtualUsage;
import com.ruoyi.project.modules.interfaceinfo.service.IInterfaceInfoService;
import com.ruoyi.project.modules.interfaceinfo.service.IVirtualUsageService;
import com.ruoyi.project.modules.interfaceinfo.vo.InterfaceParam;
import com.ruoyi.project.modules.parameter.mapper.InterfaceParamsFieldRelationMapper;
import com.ruoyi.project.modules.table.domain.FieldInfo;
import com.ruoyi.project.modules.table.service.ITableInfoService;
import com.ruoyi.project.modules.virtualUsageGroup.domain.VirtualUsageGroup;
import com.ruoyi.project.modules.virtualUsageGroup.service.IVirtualUsageGroupService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.modules.interfaceModel.mapper.InterfaceModelMapper;
import com.ruoyi.project.modules.interfaceModel.domain.InterfaceModel;
import com.ruoyi.project.modules.interfaceModel.service.IInterfaceModelService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 接口模式统计Service业务层处理
 *
 * @author smallrain
 * @date 2022-06-11
 */
@Service
public class InterfaceModelServiceImpl implements IInterfaceModelService {
    @Resource
    private InterfaceModelMapper interfaceModelMapper;

    /**
     * 查询接口模式统计
     *
     * @param id 接口模式统计主键
     * @return 接口模式统计
     */
    @Override
    public InterfaceModel selectInterfaceModelById(Long id) {
        return interfaceModelMapper.selectInterfaceModelById(id);
    }

    /**
     * 查询接口模式统计列表
     *
     * @param interfaceModel 接口模式统计
     * @return 接口模式统计
     */
    @Override
    public List<InterfaceModel> selectInterfaceModelList(InterfaceModel interfaceModel) {
        return interfaceModelMapper.selectInterfaceModelList(interfaceModel);
    }

    /**
     * 新增接口模式统计
     *
     * @param interfaceModel 接口模式统计
     * @return 结果
     */
    @Override
    public int insertInterfaceModel(InterfaceModel interfaceModel) {
        return interfaceModelMapper.insertInterfaceModel(interfaceModel);
    }

    /**
     * 修改接口模式统计
     *
     * @param interfaceModel 接口模式统计
     * @return 结果
     */
    @Override
    public int updateInterfaceModel(InterfaceModel interfaceModel) {
        return interfaceModelMapper.updateInterfaceModel(interfaceModel);
    }

    /**
     * 批量删除接口模式统计
     *
     * @param ids 需要删除的接口模式统计主键
     * @return 结果
     */
    @Override
    public int deleteInterfaceModelByIds(String ids) {
        return interfaceModelMapper.deleteInterfaceModelByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除接口模式统计信息
     *
     * @param id 接口模式统计主键
     * @return 结果
     */
    @Override
    public int deleteInterfaceModelById(Long id) {
        return interfaceModelMapper.deleteInterfaceModelById(id);
    }

    @Resource
    private ISysDictTypeService sysDictTypeService;
    @Resource
    private InterfaceParamsFieldRelationMapper interfaceParamsFieldRelationMapper;
    @Resource
    private ITableInfoService tableInfoService;
    @Resource
    private IInterfaceInfoService interfaceInfoService;
    @Resource
    private IVirtualUsageService virtualUsageService;
    @Resource
    private IVirtualUsageGroupService virtualUsageGroupService;
    @Resource
    private ISysDictDataService sysDictDataService;

    private AtomicBoolean isStart = new AtomicBoolean(false);
    @Override
    public void refresh() {
        try {
            if (isStart.compareAndSet(false, true)) {
                this.interfaceModelMapper.deleteInterfaceModel();

                List<SysDictData> pkType = this.sysDictTypeService.selectDictDataByType("pkType");
                List<SysDictData> fkType = this.sysDictTypeService.selectDictDataByType("fkType");
                List<SysDictData> timeType = this.sysDictTypeService.selectDictDataByType("timeType");
                List<SysDictData> otherType = this.sysDictTypeService.selectDictDataByType("otherType");
                List<SysDictData> fieldOtherModel = this.sysDictTypeService.selectDictDataByType("fieldOtherModel");
                List<SysDictData> yesFrontNoBack = this.sysDictTypeService.selectDictDataByType("isAllowEmpty");
                List<SysDictData> virtualUsageModel = this.sysDictTypeService.selectDictDataByType("virtualUsageModel");
                Map<String, List<SysDictData>> pkMap = pkType.stream().collect(Collectors.groupingBy(SysDictData::getDictValue));
                Map<String, List<SysDictData>> fkMap = fkType.stream().collect(Collectors.groupingBy(SysDictData::getDictValue));
                Map<String, List<SysDictData>> timeMap = timeType.stream().collect(Collectors.groupingBy(SysDictData::getDictValue));
                Map<String, List<SysDictData>> otherMap = otherType.stream().collect(Collectors.groupingBy(SysDictData::getDictValue));
                Map<String, List<SysDictData>> fieldOtherModelMap = fieldOtherModel.stream().collect(Collectors.groupingBy(SysDictData::getDictValue));
                Map<String, List<SysDictData>> yesFrontNoBackMap = yesFrontNoBack.stream().collect(Collectors.groupingBy(SysDictData::getDictValue));
                Map<String, List<SysDictData>> virtualUsageModelMap = virtualUsageModel.stream().collect(Collectors.groupingBy(SysDictData::getDictValue));
                List<InterfaceParam> interfaceParams = this.interfaceParamsFieldRelationMapper.fieldModelList();
                for (InterfaceParam interfaceParam : interfaceParams) {
                    if ("1".equals(interfaceParam.getType()) || "2".equals(interfaceParam.getType()) || "3".equals(interfaceParam.getType()) || "4".equals(interfaceParam.getType())) {
                        if ("3".equals(interfaceParam.getType())) {
                            VirtualUsage virtualUsage = this.virtualUsageService.selectVirtualUsageById(interfaceParam.getInterfaceId());
                            if (virtualUsage == null) {
                                continue;
                            }
                            interfaceParam.setInterfaceId(virtualUsage.getRelationInterfaceId());
                        }
                        List<FieldInfo> fieldInfoList = this.tableInfoService.selectFieldInfoByFieldId(Arrays.asList(interfaceParam.getFieldId()));
                        FieldInfo fieldInfo = fieldInfoList.get(0);
                        SysDictData sysDictData = null;
                        if (StringUtils.isNotEmpty(interfaceParam.getDesignContent())) {
                            if (fieldInfo.getFieldType().equals("1")) {
                                List<SysDictData> sysDictDataList = pkMap.get(interfaceParam.getDesignContent());
                                if (CollectionUtil.isNotEmpty(sysDictDataList)) {
                                    sysDictData = sysDictDataList.get(0);
                                }
                            } else if (fieldInfo.getFieldType().equals("2")) {
                                List<SysDictData> sysDictDataList = fkMap.get(interfaceParam.getDesignContent());
                                if (CollectionUtil.isNotEmpty(sysDictDataList)) {
                                    sysDictData = sysDictDataList.get(0);
                                }
                            } else if (fieldInfo.getIsFieldEnum().equals("Y")) {
                                List<SysDictData> sysDictDatas = new ArrayList<>();
                                if (fieldInfo.getDictId() != null) {
                                    SysDictType sysDictType = this.sysDictTypeService.selectDictTypeById(fieldInfo.getDictId());
                                    if (sysDictType != null) {
                                        SysDictData dictData = new SysDictData();
                                        dictData.setDictType(sysDictType.getDictType());
                                        sysDictDatas = this.sysDictDataService.selectDictDataList(dictData);
                                        if (CollectionUtil.isNotEmpty(sysDictDatas)) {
                                            for (SysDictData data : sysDictDatas) {
                                                if (interfaceParam.getDesignContent().equals(data.getDictValue())) {
                                                    sysDictData = data;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            } else if (fieldInfo.getFieldDataType().equals("3")) {
                                List<SysDictData> sysDictDataList = timeMap.get(interfaceParam.getDesignContent());
                                if (CollectionUtil.isNotEmpty(sysDictDataList)) {
                                    sysDictData = sysDictDataList.get(0);
                                }
                            } else {
                                List<SysDictData> sysDictDataList = otherMap.get(interfaceParam.getDesignContent());
                                if (CollectionUtil.isNotEmpty(sysDictDataList)) {
                                    sysDictData = sysDictDataList.get(0);
                                }
                            }
                        }

                        if (sysDictData != null) {
                            InterfaceModel interfaceModel = new InterfaceModel();
                            interfaceModel.setInterfaceId(interfaceParam.getInterfaceId());
                            interfaceModel.setModelType("1");
                            interfaceModel.setModelName(sysDictData.getDictLabel());
                            interfaceModel.setModelEnName(sysDictData.getDictValue());
                            interfaceModel.setIsInParam("1");
                            this.interfaceModelMapper.insertInterfaceModel(interfaceModel);
                        }
                        if (StringUtils.isNotEmpty(interfaceParam.getFieldDescription())) {
                            InterfaceModel interfaceModel = new InterfaceModel();
                            interfaceModel.setInterfaceId(interfaceParam.getInterfaceId());
                            interfaceModel.setModelType("2");
                            interfaceModel.setIsInParam("1");
                            interfaceModel.setModelName(interfaceParam.getFieldDescription());
                            interfaceModel.setModelEnName(interfaceParam.getFieldDescription());
                            this.interfaceModelMapper.insertInterfaceModel(interfaceModel);
                        }
                        if (StringUtils.isNotEmpty(interfaceParam.getAliasZhName())) {
                            InterfaceModel interfaceModel = new InterfaceModel();
                            interfaceModel.setInterfaceId(interfaceParam.getInterfaceId());
                            interfaceModel.setModelType("3");
                            interfaceModel.setIsInParam("1");
                            interfaceModel.setModelName(interfaceParam.getAliasZhName());
                            interfaceModel.setModelEnName(interfaceParam.getAliasZhName());
                            this.interfaceModelMapper.insertInterfaceModel(interfaceModel);
                        }
                        if (StringUtils.isNotEmpty(interfaceParam.getAliasName())) {
                            InterfaceModel interfaceModel = new InterfaceModel();
                            interfaceModel.setInterfaceId(interfaceParam.getInterfaceId());
                            interfaceModel.setModelType("4");
                            interfaceModel.setIsInParam("1");
                            interfaceModel.setModelName(interfaceParam.getAliasName());
                            interfaceModel.setModelEnName(interfaceParam.getAliasName());
                            this.interfaceModelMapper.insertInterfaceModel(interfaceModel);
                        }

                        if (StringUtils.isNotEmpty(interfaceParam.getDictValue()) && StringUtils.isNotEmpty(interfaceParam.getDictType())) {
                            List<SysDictData> sysDictData1 = this.sysDictTypeService.selectDictDataByType(interfaceParam.getDictType());
                            if (CollectionUtil.isNotEmpty(sysDictData1)) {
                                for (SysDictData data : sysDictData1) {
                                    if (interfaceParam.getDictValue().equals(data.getDictValue())) {
                                        InterfaceModel interfaceModel = new InterfaceModel();
                                        interfaceModel.setInterfaceId(interfaceParam.getInterfaceId());
                                        interfaceModel.setModelType("9");
                                        interfaceModel.setIsInParam("1");
                                        interfaceModel.setModelName(data.getDictLabel());
                                        interfaceModel.setModelEnName(data.getDictValue());
                                        this.interfaceModelMapper.insertInterfaceModel(interfaceModel);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                interfaceParams = this.interfaceParamsFieldRelationMapper.fieldModelListByOutParam();
                for (InterfaceParam interfaceParam : interfaceParams) {
                    if ("1".equals(interfaceParam.getType()) || "2".equals(interfaceParam.getType()) || "3".equals(interfaceParam.getType()) || "4".equals(interfaceParam.getType())) {
                        if ("3".equals(interfaceParam.getType())) {
                            VirtualUsage virtualUsage = this.virtualUsageService.selectVirtualUsageById(interfaceParam.getInterfaceId());
                            if (virtualUsage == null) {
                                continue;
                            }
                            interfaceParam.setInterfaceId(virtualUsage.getRelationInterfaceId());
                        }
                        if (StringUtils.isNotEmpty(interfaceParam.getAliasZhName())) {
                            InterfaceModel interfaceModel = new InterfaceModel();
                            interfaceModel.setInterfaceId(interfaceParam.getInterfaceId());
                            interfaceModel.setModelType("3");
                            interfaceModel.setIsInParam("2");
                            interfaceModel.setModelName(interfaceParam.getAliasZhName());
                            interfaceModel.setModelEnName(interfaceParam.getAliasZhName());
                            this.interfaceModelMapper.insertInterfaceModel(interfaceModel);
                        }
                        if (StringUtils.isNotEmpty(interfaceParam.getAliasName())) {
                            InterfaceModel interfaceModel = new InterfaceModel();
                            interfaceModel.setInterfaceId(interfaceParam.getInterfaceId());
                            interfaceModel.setModelType("4");
                            interfaceModel.setIsInParam("2");
                            interfaceModel.setModelName(interfaceParam.getAliasName());
                            interfaceModel.setModelEnName(interfaceParam.getAliasName());
                            this.interfaceModelMapper.insertInterfaceModel(interfaceModel);
                        }
                        if (StringUtils.isNotEmpty(interfaceParam.getIsAllowEmpty())) {
                            List<SysDictData> sysDictData = yesFrontNoBackMap.get(interfaceParam.getIsAllowEmpty());
                            if (CollectionUtil.isNotEmpty(sysDictData)) {
                                InterfaceModel interfaceModel = new InterfaceModel();
                                interfaceModel.setInterfaceId(interfaceParam.getInterfaceId());
                                interfaceModel.setModelType("6");
                                interfaceModel.setIsInParam("2");
                                interfaceModel.setModelName(sysDictData.get(0).getDictLabel());
                                interfaceModel.setModelEnName(sysDictData.get(0).getDictValue());
                                this.interfaceModelMapper.insertInterfaceModel(interfaceModel);
                            }
                        }
                        if (StringUtils.isNotEmpty(interfaceParam.getOtherModel())) {
                            List<SysDictData> sysDictData = fieldOtherModelMap.get(interfaceParam.getOtherModel());
                            if (CollectionUtil.isNotEmpty(sysDictData)) {
                                InterfaceModel interfaceModel = new InterfaceModel();
                                interfaceModel.setInterfaceId(interfaceParam.getInterfaceId());
                                interfaceModel.setModelType("5");
                                interfaceModel.setIsInParam("2");
                                interfaceModel.setModelName(sysDictData.get(0).getDictLabel());
                                interfaceModel.setModelEnName(sysDictData.get(0).getDictValue());
                                this.interfaceModelMapper.insertInterfaceModel(interfaceModel);
                            }
                        }
                        if (StringUtils.isNotEmpty(interfaceParam.getFieldDescription())) {
                            InterfaceModel interfaceModel = new InterfaceModel();
                            interfaceModel.setInterfaceId(interfaceParam.getInterfaceId());
                            interfaceModel.setModelType("2");
                            interfaceModel.setIsInParam("2");
                            interfaceModel.setModelName(interfaceParam.getFieldDescription());
                            interfaceModel.setModelEnName(interfaceParam.getFieldDescription());
                            this.interfaceModelMapper.insertInterfaceModel(interfaceModel);
                        }
                    }
                }

                List<VirtualUsage> virtualUsages = this.virtualUsageService.selectVirtualUsageList(new VirtualUsage());
                if (CollectionUtil.isNotEmpty(virtualUsages)) {
                    for (VirtualUsage virtualUsage : virtualUsages) {
                        if (StringUtils.isNotEmpty(virtualUsage.getModel())) {
                            InterfaceModel interfaceModel = new InterfaceModel();
                            interfaceModel.setInterfaceId(virtualUsage.getRelationInterfaceId());
                            List<SysDictData> sysDictData = virtualUsageModelMap.get(virtualUsage.getModel());
                            if (CollectionUtil.isNotEmpty(sysDictData)) {
                                interfaceModel.setModelType("7");
                                interfaceModel.setIsInParam("3");
                                interfaceModel.setModelName(sysDictData.get(0).getDictLabel());
                                interfaceModel.setModelEnName(sysDictData.get(0).getDictValue());
                                this.interfaceModelMapper.insertInterfaceModel(interfaceModel);
                            }
                        }
                        if (StringUtils.isNotEmpty(virtualUsage.getIsAllowEmpty())) {
                            InterfaceModel interfaceModel = new InterfaceModel();
                            interfaceModel.setInterfaceId(virtualUsage.getRelationInterfaceId());
                            List<SysDictData> sysDictData = yesFrontNoBackMap.get(virtualUsage.getIsAllowEmpty());
                            if (CollectionUtil.isNotEmpty(sysDictData)) {
                                interfaceModel.setModelType("8");
                                interfaceModel.setIsInParam("3");
                                interfaceModel.setModelName(sysDictData.get(0).getDictLabel());
                                interfaceModel.setModelEnName(sysDictData.get(0).getDictValue());
                                this.interfaceModelMapper.insertInterfaceModel(interfaceModel);
                            }
                        }
                    }
                }
            }
        } finally {
            isStart.compareAndSet(true, false);
        }
    }

    @Override
    public List<InterfaceModel> selectInterfaceModelList2(InterfaceModel interfaceModel) {
        return this.interfaceModelMapper.selectInterfaceModelList2(interfaceModel);
    }
}